<?php
class Model_Ebaygenerator {
	
	
	public static function  prepareEbayFile($dateToday = null){
	
		if($dateToday==""){
				
			$dateToday = Model_DatetimeHelper::dateToday();
		}
	
		$ebayTitle= "Action(CC=Cp1252),SiteID,Format,Title,Condition,SubTitle,Custom Label,Category,Category2,StoreCategory,StoreCategory2,Quantity,LotSize,Currency,StartPrice,BuyItNowPrice,ReservePrice,InsuranceOption,InsuranceFee,DomesticInsuranceOption,DomesticInsuranceFee,PackagingHandlingCosts,InternationalPackagingHandlingCosts,Duration,PrivateAuction,Country,ProductIDType,ProductIDValue,ItemID,Description,Counter,PicURL,BoldTitle,Featured,GalleryType,FeaturedFirstDuration,Highlight,Border,HomePageFeatured,Subtitle in search resutls,GiftIcon,GiftServices-1,GiftServices-2,GiftServices-3,SalesTaxPercent,SalesTaxState,ShippingInTax,UseTaxTable,PostalCode,ProxyItem,VATPercent,Location,ImmediatePayRequired,PayPalAccepted,PayPalEmailAddress,PaymentInstructions,CashOnPickupAccepted,CCAccepted,AmEx,Discover,VisaMastercard,IntegratedMerchantCreditCard,COD,CODPrePayDelivery,PostalTransfer,MOCashiers,PersonalCheck,MoneyXferAccepted,MoneyXferAcceptedinCheckout,PaymentOther,OtherOnlinePayments,PaymentSeeDescription,Escrow,ShippingType,ShipFromZipCode,ShippingIrregular,ShippingPackage,WeightMajor,WeightMinor,WeightUnit,MeasurementUnit,ShippingDetails/CODCost,PackageLength,PackageWidth,PackageDepth,CharityID,CharityName,DonationPercent,ShippingService-1:Option,ShippingService-1:Cost,ShippingService-1:AdditionalCost,ShippingService-1:Priority,ShippingService-1:FreeShipping,ShippingService-1:ShippingSurcharge,ShippingService-2:Option,ShippingService-2:Cost,ShippingService-2:AdditionalCost,ShippingService-2:Priority,ShippingService-2:ShippingSurcharge,ShippingService-3:Option,ShippingService-3:Cost,ShippingService-3:AdditionalCost,ShippingService-3:Priority,ShippingService-3:ShippingSurcharge,ShippingService-4:Option,ShippingService-4:Cost,ShippingService-4:AdditionalCost,ShippingService-4:Priority,ShippingService-4:ShippingSurcharge,ShippingService-5:Option,ShippingService-5:Cost,ShippingService-5:AdditionalCost,ShippingService-5:Priority,ShippingService-5:ShippingSurcharge,GetItFast,DispatchTimeMax,IntlShippingService-1:Option,IntlShippingService-1:Cost,IntlShippingService-1:AdditionalCost,IntlShippingService-1:Locations,IntlShippingService-1:Priority,IntlShippingService-2:Option,IntlShippingService-2:Cost,IntlShippingService-2:AdditionalCost,IntlShippingService-2:Locations,IntlShippingService-2:Priority,IntlShippingService-3:Option,IntlShippingService-3:Cost,IntlShippingService-3:AdditionalCost,IntlShippingService-3:Locations,IntlShippingService-3:Priority,IntlShippingService-4:Option,IntlShippingService-4:Cost,IntlShippingService-4:AdditionalCost,IntlShippingService-4:Locations,IntlShippingService-4:Priority,IntlShippingService-5:Option,IntlShippingService-5:Cost,IntlShippingService-5:AdditionalCost,IntlShippingService-5:Locations,IntlShippingService-5:Priority,IntlAddnlShiptoLocations,PaisaPayAccepted,PaisaPay EMI payment,BasicUpgradePackBundle,ValuePackBundle,ProPackPlusBundle,BestOfferEnabled,AutoAccept,BestOfferAutoAcceptPrice,AutoDecline,MinBestOfferPrice,BestOfferRejectMessage,LocalOnlyChk,LocalListingDistance,BuyerRequirements:ShipToRegCountry,BuyerRequirements:ZeroFeedbackScore,BuyerRequirements:MinFeedbackScore,BuyerRequirements:MaxUnpaidItemsCount,BuyerRequirements:MaxUnpaidItemsPeriod,BuyerRequirements:MaxItemCount,BuyerRequirements:MaxItemMinFeedback,BuyerRequirements:LinkedPayPalAccount,BuyerRequirements:VerifiedUser,BuyerRequirements:VerifiedUserScore,BuyerRequirements:MaxViolationCount,BuyerRequirements:MaxViolationPeriod,SellerDetails:PrimaryPhone,SellerDetails:SecondaryPhone,ExtSellerDetails:Hours1Days,ExtSellerDetails:Hours1AnyTime,ExtSellerDetails:Hours1From,ExtSellerDetails:Hours1To,ExtSellerDetails:Hours2Days,ExtSellerDetails:Hours2AnyTime,ExtSellerDetails:Hours2From,ExtSellerDetails:Hours2To,ExtSellerDetails:TimeZoneID,ListingDesigner:LayoutID,ListingDesigner:ThemeID,ProStores Name,ProStores Enabled,Domestic Profile Discount,International Profile Discount,Apply Profile Domestic,Apply Profile International,PromoteCBT,ShipToLocations,CustomLabel,CashOnPickup,ReturnsAcceptedOption,ReturnsWithinOption,RefundOption,ShippingCostPaidBy,WarrantyOffered,WarrantyType,WarrantyDuration,AdditionalDetails,MarketplaceType,ProjectGoodCategory,ShortDescription,ProducerDescription,RegionOfOrigin,ProducerPhotoURL\n\r";
		$ebayTitleArray= explode(",",$ebayTitle);
	
		$resultData = array_fill(0,203,"");
		$folder = getcwd()."/export/ebay/";
		$ebayFileName = "ebaylisting".$dateToday.".csv";
		if(!file_exists($folder.$ebayFileName)){
	
			$handle=fopen($folder.$ebayFileName,"w");
			fputcsv($handle,$ebayTitleArray);
			fclose($handle);
		}
	
		return array($folder,$ebayFileName);
	}
	/**
	 *  To generate eBay Turob lister  csv file 
	 *  
	 * @param unknown_type $productTitle
	 * @param unknown_type $productCode
	 * @param unknown_type $ebayCategory
	 * @param unknown_type $storeCategory
	 * @param unknown_type $qty
	 * @param unknown_type $ebayPrice
	 * @param unknown_type $description
	 * @param unknown_type $imageIndividual
	 * @param unknown_type $imageWhole
	 * @param unknown_type $paypalAccount
	 * @param unknown_type $postage
	 * @param unknown_type $productFeature
	 * @param unknown_type $sizeMM
	 * @param unknown_type $weightKg
	 * @param unknown_type $templateStr
	 */

	public function generateEbayCsv($productTitle,$productCode,$ebayCategory,$storeCategory,$qty,$ebayPrice,$description,$imageIndividual,$imageWhole,$paypalAccount,$postage,$productFeature,$sizeMM,$weightKg,$templateStr){
		
		$resultData = array_fill(0,203,"");
		
		//Action
		$resultData[0]="Add";
		// Site
		$resultData[1]="Australia";
		// Format
		$resultData[2]="FixedPriceItem";
		//Product Title 
		$product_title= str_replace("\n","",$productTitle);
		$resultData[3]=$product_title;

		//condition ,everything is brand new
		$resultData[4]="1000";
		
		$customLabel = $productCode;
		$resultData[6] = $customLabel;

		//ebay Category
		$resultData[7] = $ebayCategory;

		//Store Category
		$resultData[9] = $storeCategory;
		// Qty
		$resultData[11] = $qty;
		
		//Currency
		$resultData[13] = "AUD";
		
		//eBay Price
		$resultData[14] = $ebayPrice;
		$resultData[15] = $ebayPrice + 1;

		//Duration
		$resultData[23]="GTC";
		//PrivateAuction
		$resultData[24]="0";
		//Country
		$resultData[25]="AU";

		//descripton 
		//$longdes=$description;



		//$colorAvailable = $_POST['color_avail'];

		$productFeature = nl2br($productFeature);

		$thetemplate = str_replace("\n","",$templateStr);
		$thetemplate = str_replace("\r","",$templateStr);

//$resultData[29] =  desbuilder($template,$product_title,$ebay_gallery_img,$long_des,$color_avail,$des_img,$pro_feature,$screen_img)
		$resultData[29] = self::desbuilder($templateStr,$productTitle,$imageIndividual,$imageWhole,$description,$productFeature,$sizeMM,$weightKg,$productCode);
		//picurl
		if($imageIndividual!=""){

		$resultData[31]= $imageIndividual;
		//GalleryType
		$resultData[34]="Gallery";
			}
//Postcode
$resultData[48]="3168";
//paypalaccept
$resultData[53]="1";
//paypalemil
$resultData[54]=$paypalAccount;
//MoneyXferAccepted
//$resultData[67]="1";
//$resultData[68]="1";
//shippingtype
$resultData[73]="Flat";
//ShippingService-1:Option
$resultData[88]="AU_Regular";
//ShippingService-1:Cost
$resultData[89]=$postage;
//ShippingService-1:AdditionalCost
$resultData[90]=$postage;

$resultData[91]="1";

$resultData[92]=($postage == 0)?"1":"0";

if($ebayPrice>100)
{
$resultData[94]="AU_Pickup";
$resultData[95]="5";
$resultData[96]="0";
$resultData[97]="2";
}
$resultData[114]="0";
$resultData[115]="3"; // handling time 

//$resultData[122]="0";
//$resultData[123]="3"; //additional ?

$resultData[190]="ReturnsAccepted";
$resultData[191]="Days_7";
$resultData[192]="Exchange";
$resultData[193]="Buyer";
//
$resultData[203]="\n\r";


return $resultData;
			
	}
	/**
	 * To build the ebay listing template using the template already there 
	 * @param unknown_type $template
	 * @param unknown_type $productTitle
	 * @param unknown_type $imgIndividual
	 * @param unknown_type $imgWhole
	 * @param unknown_type $longdes
	 * @param unknown_type $productFeature
	 * @param unknown_type $sizeMM
	 * @param unknown_type $weight
	 * @param unknown_type $barCode
	 */
	
	public function desbuilder($template,$productTitle,$imgIndividual,$imgWhole,$longdes,$productFeature,$sizeMM,$weight,$barCode){
		$imgOther = '';
		$shortImgInd = substr($imgIndividual,0,-5);
		
		// check if 1 2 exist 
		//http://www.lovebargain.com.au/product_img/home_img/APIPH6HC102-0_1.jpg
		//$fl = new Model_Fileshandler();
		$imgFolder = getcwd().'/product_img/home_img/';
		$arrShortImgInd = explode('/', $shortImgInd);
		$strImg1 = end($arrShortImgInd).'1.jpg';
		$strImg2 = end($arrShortImgInd).'2.jpg';
		if(file_exists($imgFolder.$strImg1)){
			
			$imgOther .='<img src="http://www.lovebargain.com.au/product_img/home_img/'.$strImg1.'"  width="600" height="600" /><br />';
		}
		if(file_exists($imgFolder.$strImg2)){
				
			$imgOther .='<img src="http://www.lovebargain.com.au/product_img/home_img/'.$strImg2.'" width="600" height="600" /><br />';
		}
		
		$finaldes = $template;
		$finaldes =str_replace("(product_title)",$productTitle,$finaldes);
		$finaldes =str_replace("(ebay_gallery_img)",$imgIndividual,$finaldes);
		$finaldes =str_replace("(img_whole)",$imgWhole,$finaldes);
		$finaldes =str_replace("(long_des)",$longdes,$finaldes);
		$finaldes =str_replace("(pro_feature)",$productFeature,$finaldes);
		$finaldes =str_replace("(size_mm)",$sizeMM,$finaldes);
		$finaldes =str_replace("(weight_kg)",$weight,$finaldes);
		$finaldes =str_replace("(bar_code)",$barCode,$finaldes);
		$finaldes =str_replace("(img_other)",$imgOther,$finaldes);
		
		$finaldes = str_replace("\n","",$finaldes);
		$finaldes = str_replace("\r","",$finaldes);
		return $finaldes;
		
		
	}
	/**
	 * read exist template
	 */
	
	public function prepareTemplate(){
		
		$fl = fopen("kmobiletemplate1.html","r");
		$str = fread($fl, filesize("kmobiletemplate1.html"));
		fclose($fl);
		
		return $str;
	}
}

?>